package sd.czw.com.mmgapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import okhttp3.Call;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.model.loginandsign.LoginNormalCallback;
import sd.czw.com.mmgapp.model.loginandsign.LoginNormalModel;

/**
 * Created by czw
 * desc 描述：
 * on 2017/7/14.
 */
public class GetXGCodeActivity extends AppCompatActivity {
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.edt_numbers)
    EditText edtNumbers;
    @Bind(R.id.getphonenumber)
    Button getphonenumber;
    @Bind(R.id.ll_xy)
    LinearLayout ll_xy;
    private String numbers;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_getcode);
        ButterKnife.bind(this);
        ll_xy.setVisibility(View.GONE);
    }
    @OnTextChanged(value = R.id.edt_numbers,callback = OnTextChanged.Callback.TEXT_CHANGED)
    void onTextChanged(CharSequence s, int start, int before, int count) {
        numbers = edtNumbers.getText().toString();
        if (numbers != null && !("".equals(numbers)) && numbers.length() == 11){
            getphonenumber.setBackground(getDrawable(R.drawable.login_checkbtn));
        }else {
            getphonenumber.setBackground(getDrawable(R.drawable.login_btn));
        }
    }

    @OnClick({R.id.rl_back,R.id.getphonenumber})
    public void onclick(View view){
        switch (view.getId()){
            case R.id.rl_back:
                onBackPressed();
                break;
            case R.id.getphonenumber:
                //startActivity(new Intent(this,CodeActivity.class));
                final String s = edtNumbers.getText().toString();
                OkHttpUtils.post().url(Contants.NMLOGIN).addParams("mobile_phone",s).addParams("act","yan")
                        .addParams("time",Contants.Time).addParams("sign",Contants.Sign).build()
                        .execute(new LoginNormalCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {

                            }

                            @Override
                            public void onResponse(LoginNormalModel response, int id) {
                                if (response.getErrcode() == 1){
                                    Intent intent = new Intent(GetXGCodeActivity.this, CodeActivity.class);
                                    intent.putExtra("phonenumber",s);
                                    startActivity(intent);
                                }else if (response.getErrcode() == 1002){
                                    Toast.makeText(GetXGCodeActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(GetXGCodeActivity.this, ""+response.getErrmsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
